package com.wms.function.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.wms.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wms.common.annotation.Log;
import com.wms.common.core.controller.BaseController;
import com.wms.common.core.domain.AjaxResult;
import com.wms.common.enums.BusinessType;
import com.wms.function.domain.TbSpu;
import com.wms.function.service.ITbSpuService;
import com.wms.common.utils.poi.ExcelUtil;

/**
 * 商品Controller
 *
 * @author ruoyi
 * @date 2025-02-21
 */
@RestController
@RequestMapping("/function/spu")
@Api(tags = "商品的接口")
public class TbSpuController extends BaseController {
    @Autowired
    private ITbSpuService tbSpuService;

    /**
     * 查询商品列表
     */
    @ApiOperation("查询商品列表")
    @PreAuthorize("@ss.hasPermi('function:spu:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbSpu tbSpu) {
        List<TbSpu> list = tbSpuService.selectTbSpuList(tbSpu);
//            return success(list);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @ApiOperation("导出商品列表")
    @PreAuthorize("@ss.hasPermi('function:spu:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbSpu tbSpu) {
        List<TbSpu> list = tbSpuService.selectTbSpuList(tbSpu);
        ExcelUtil<TbSpu> util = new ExcelUtil<TbSpu>(TbSpu.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @ApiOperation("获取商品详细信息")
    @PreAuthorize("@ss.hasPermi('function:spu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(value = "商品ID", required = true)
                              @PathVariable("id") Long id) {
        return success(tbSpuService.selectTbSpuById(id));
    }

    /**
     * 新增商品
     */
    @ApiOperation("新增商品")
    @PreAuthorize("@ss.hasPermi('function:spu:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam(value = "商品实体")
                          @RequestBody TbSpu tbSpu) {
        return toAjax(tbSpuService.insertTbSpu(tbSpu));
    }

    /**
     * 修改商品
     */
    @ApiOperation("修改商品")
    @PreAuthorize("@ss.hasPermi('function:spu:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@ApiParam(value = "商品实体")
                           @RequestBody TbSpu tbSpu) {
        return toAjax(tbSpuService.updateTbSpu(tbSpu));
    }

    /**
     * 删除商品
     */
    @ApiOperation("删除商品")
    @PreAuthorize("@ss.hasPermi('function:spu:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tbSpuService.deleteTbSpuByIds(ids));
    }
}
